/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package AccManager;

import java.io.*;
import java.util.*;
import java.net.*;
import java.util.ArrayList;

/**
 *
 * @author lcy
 */

public class UserProfile implements Serializable{

    // 15 fields
    private String _userID;
    private String _passwd_raw;
    private String _avatarID;
    private int _Alevel;
    private int _Dlevel;
    private int _coins;
    private int _exp;
    private int _magicPoint;
    private int [] _capa;
    private int _gender;
    private UserStatus _status;
    private int _time_to_live;
    private InetAddress _usr_addr; //Or ip -lcy
    private int _port;
    private ArrayList<String> _contactList;


    public UserProfile(){
        _userID = "player";
        _passwd_raw = "abcde";
        _usr_addr = null;
        _port = -1;
        InitDefaultValues();
        InitCommComponents();       
    }

    public UserProfile(String name, String passwd){
        _userID = name;
        _passwd_raw = passwd;
        _usr_addr = null;
        _port = -1;
        InitDefaultValues();
        InitCommComponents();

    }

    /*
    public UserProfile(String name, String passwd, UserStatus status){
        _userID = name;
        _passwd_raw = passwd;
        _status = status;
        _usr_addr = null;
        _port = -1;
        
        InitDefaultValues();
        InitCommComponents();
    }
     */

    public UserProfile(String name, String passwd, InetAddress address, int port)throws UnknownHostException
    {
        _userID = name;
        _passwd_raw = passwd;

        try{
            _usr_addr = InetAddress.getByName(address.getHostName());
        }catch(UnknownHostException e){
            throw e;
        }

        _port = port;
        InitDefaultValues();
        InitCommComponents();
    }

    public UserProfile(String uID, String passw, int gen, String ip, int pt) throws UnknownHostException {
        _userID = uID;
        _passwd_raw = passw;
        _gender = gen;
        _port = pt;

        try{
            _usr_addr = InetAddress.getByName(ip);
        }catch(UnknownHostException e){
            throw e;
        }

        _avatarID = "";
        _Alevel = 1;
        _Dlevel = 1;
        _coins = 0;
        _exp = 0;
        _magicPoint = 100;
        _capa = new int[]{0, 0, 0, 0, 0, 0, 0, 0};
        _time_to_live =0;
        _status = UserStatus.OFFLINE; //online
        _contactList = new ArrayList<String>();
    }

    private void InitDefaultValues(){
        _avatarID = "";
        _Alevel = 1;
        _Dlevel = 1;
        _coins = 0;
        _exp = 0;
        _magicPoint = 100;
        _capa = new int[]{0, 0, 0, 0, 0, 0, 0, 0};
        _time_to_live =0;
        _status = UserStatus.OFFLINE; //online
        _contactList = new ArrayList<String>();
        _gender = 0;
    }

    private void InitCommComponents(){
        if(_status == UserStatus.OFFLINE)
            _time_to_live = -1;
    }

    public void copy(UserProfile newP){
        this._Alevel = newP._Alevel;
        this._Dlevel = newP._Dlevel;
        this._avatarID = newP._avatarID;
        this._capa = newP._capa;
        this._coins = newP._coins;
        this._exp = newP._exp;
        this._gender = newP._gender;
        this._magicPoint = newP._magicPoint;
        this._passwd_raw = newP._passwd_raw;
        this._port = newP._port;
        this._status = newP._status;
        this._time_to_live = newP._time_to_live;
        this._userID = newP._userID;
        this._usr_addr = newP._usr_addr;
        this._contactList = new ArrayList<String>();

        if(newP._contactList != null && newP._contactList.size() > 0){

            CopyContactList(newP._contactList);
        }else{
            this._contactList = new ArrayList<String>();
        }

    }

    public UserProfile(UserProfile newP){
        this._Alevel = newP._Alevel;
        this._Dlevel = newP._Dlevel;
        this._avatarID = newP._avatarID;
        this._capa = newP._capa;
        this._coins = newP._coins;
        this._exp = newP._exp;
        this._gender = newP._gender;
        this._magicPoint = newP._magicPoint;
        this._passwd_raw = newP._passwd_raw;
        this._port = newP._port;
        this._status = newP._status;
        this._time_to_live = newP._time_to_live;
        this._userID = newP._userID;
        this._usr_addr = newP._usr_addr;
        this._contactList = new ArrayList<String>();

        if(newP._contactList != null && newP._contactList.size() > 0){
            
            CopyContactList(newP._contactList);
        }else{
            this._contactList = new ArrayList<String>();
        }

    }

    /// Pre: list is not null and size is not 0;
    private void CopyContactList(ArrayList<String> list){
        this._contactList = new ArrayList<String>();
        for(int i = 0; i < list.size(); i ++){
            this._contactList.add(list.get(i));
        }

    }

    public boolean IsBiggerThan(UserProfile other, CompareRule rule){

        switch(rule){
            case NAME:
                if((this._userID).compareToIgnoreCase(other.GetUserID()) <= 0)
                    return false;
                break;
        }

        return true;
    }

    public synchronized void  SetUserID(String name){
        _userID = name;
    }

    public String GetUserID(){

        return _userID;
    }

    public synchronized void SetPasswd(String raw_passwd){
        _passwd_raw = raw_passwd;
    }

    public String GetPasswdRaw(){
        return _passwd_raw;
    }

    public String getAvatarID(){
        return _avatarID;
    }

    public void setAvatarID(String value){
        _avatarID = value;
    }

    public int getAlevel(){
        return _Alevel;
    }

    public void setAlevel(int newLevel)
    {
        _Alevel = newLevel;
    }

    public int getDLevel()
    {
        return _Dlevel;
    }

    public void setDlevel(int newLevel){
        _Dlevel = newLevel;
    }

    public int getCoins(){

        return _coins;
    }

    public void setCoins(int newCoins){
        _coins = newCoins;
    }

    public int getExp(){
	return _exp;
    }

    public void setExp(int newExp){
	_exp = newExp;
    }

    public int[] getCapaList(){
        
	int [] ret = new int[_capa.length];

        for(int idx = 0; idx < ret.length; idx ++){
            ret[idx] = _capa[idx];
        }

        return ret;
    }

    public void setCapa(int type, int lvl) throws ArrayIndexOutOfBoundsException{
        
        if(type >= _capa.length){
            throw new ArrayIndexOutOfBoundsException("capa index wrong");
        }

        _capa[type] = lvl;
    }

    public void SetUserStatus(UserStatus new_stat){
        _status = new_stat;
    }

    public UserStatus GetUserStatus(){
        return _status;
    }

    public void SetTimeToLive(int time){
        _time_to_live = time;
    }

    public int GetTimeToLive(){
        return _time_to_live;
    }

    public void DecrementTTL(){
        if(_time_to_live > 0){
            _time_to_live --;
        }
    }

    public boolean IsTimeOut(){
        return _time_to_live == 0;
    }

    public boolean IsIPAddrNull(){
        if(_usr_addr == null)
            return true;
        
        return false;
    }

    public void SetUserIP(InetAddress addr){
        _usr_addr = addr;
    }

    public InetAddress GetUserIP(){
        return _usr_addr;
    }

    public void SetUserPort(int port){
        _port = port;
    }

    public int GetUserPort(){
       return _port;
    }

    public int getGender(){
        return _gender;
    }

    public void setGender(int newGender){
        _gender = newGender;
    }

    public int GetMagicPoint(){
        return _magicPoint;
    }

    public void SetMagicPoint(int point){

        _magicPoint = point;
    }

    public ArrayList<String> GetContactList(){
        return _contactList;
    }

    public void InsertContact (String id)
    {
        _contactList.add(id);
    }

    public String FindContact (String id)
    {
        ListIterator<String> lItr=_contactList.listIterator();
        while(lItr.hasNext())
        {
            String listId= lItr.next();
            if (listId.equals(id))
                return listId;
        }
        return null;

    }

    public void DeleteContact (String id)
    {
        ListIterator<String> lItr=_contactList.listIterator();
        while(lItr.hasNext())
        {
            String listId = lItr.next();
            if (listId ==id)
                lItr.remove();
        }

    }

    public void ClearContact ()
    {
        _contactList.clear();
    }
}
